package com.woniuxy.usercoachassemble.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.usercoachassemble.commons.ToData;
import com.woniuxy.usercoachassemble.dao.CoachInfoMapper;
import com.woniuxy.usercoachassemble.dao.UserMapper;
import com.woniuxy.usercoachassemble.dao.UserinfoMapper;
import com.woniuxy.usercoachassemble.entity.Account;
import com.woniuxy.usercoachassemble.entity.User;
import com.woniuxy.usercoachassemble.entity.YogaGymSignedCoach;
import com.woniuxy.usercoachassemble.dao.YogaGymSignedCoachMapper;
import com.woniuxy.usercoachassemble.service.YogaGymSignedCoachService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2020-07-18
 */
@Service
public class YogaGymSignedCoachServiceImpl extends ServiceImpl<YogaGymSignedCoachMapper, YogaGymSignedCoach> implements YogaGymSignedCoachService {


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserinfoMapper userinfoMapper;

    @Autowired
    private CoachInfoMapper coachInfoMapper;

    @Autowired
    private YogaGymSignedCoachMapper yogaGymSignedCoachMapper;
    /**
     * @description:TODO(分页查询场馆签约教练详情)
     * @param gymId 场馆id
     * @param status 签约教练的状态
     * @param current 当前页
     * @param size  每页大小
     * @return:  com.woniuxy.usercoachassemble.commons.ToData
     * @author: Fy浪迹、
     * @date: 2020/7/18 19:37
     */
    @Override
    public ToData selectSignContractCoachByGymId(Integer gymId, Integer status, Long current, Long size) {

        QueryWrapper<YogaGymSignedCoach> yogaGymSignedCoachQueryWrapper = new QueryWrapper<>();

        if(status != null){
            yogaGymSignedCoachQueryWrapper.eq("status",status);
        }

        yogaGymSignedCoachQueryWrapper.eq("yoga_gym_id",gymId);

        Page<YogaGymSignedCoach> yogaGymSignedCoachPage = new Page<>(current,size);

        IPage<YogaGymSignedCoach> ygscPage = this.baseMapper.selectPage(yogaGymSignedCoachPage, yogaGymSignedCoachQueryWrapper);

        List<YogaGymSignedCoach> records = ygscPage.getRecords();

        if(records.size() != 0){
            for (YogaGymSignedCoach ygsc:records){
                ygsc.setUser(userMapper.selectById(ygsc.getCoachId()));
                ygsc.setUserinfo(userinfoMapper.selectById(ygsc.getCoachId()));
                ygsc.setCoachInfo(coachInfoMapper.selectById(ygsc.getCoachId()));
            }
        }

        ToData toData = new ToData();
        toData.setData(records);
        toData.SetPageByMybtisPlus(ygscPage);


        return toData;
    }

    /**
     * @description:TODO(根据场馆id和教练id查询签约信息)
     * @param gymId
     * @param coachId
     * @return:  com.woniuxy.usercoachassemble.entity.YogaGymSignedCoach
     * @author: Fy浪迹、
     * @date: 2020/7/18 20:16
     */
    @Override
    public YogaGymSignedCoach selectByCoachIdAndGymId(Integer gymId, Integer coachId) {

        return query().eq("yoga_gym_id", gymId).eq("coach_id", coachId).one();
    }

    @Override
    public boolean updataStatus(Integer coachId, Integer status) {
        boolean b = update().set("status", status).eq("coach_id", coachId).update();
        return b;
    }

    /**
     * @description:TODO(新增签约信息)
     * @param gymId
     * @param coachId
     * @param status
     * @return:  boolean
     * @author: Fy浪迹、
     * @date: 2020/7/18 21:05
     */
    @Override
    public boolean insetByGymIdAndCoachId(Integer gymId, Integer coachId, Integer status) {
        YogaGymSignedCoach yogaGymSignedCoach = new YogaGymSignedCoach();
        yogaGymSignedCoach.setYogaGymId(gymId);
        yogaGymSignedCoach.setCoachId(coachId);
        yogaGymSignedCoach.setStatus(status);


        return save(yogaGymSignedCoach);
    }


    @Override
    public List<Map<String, Object>> getCoachByUserId(int userId) {
        List<Map<String, Object>> coach = yogaGymSignedCoachMapper.getCoachByUserId(userId);
        return coach;

    }

    @Override
    public List<Map<String, Object>> getCoachByCoach(int coachId) {
        List<Map<String, Object>> coach = yogaGymSignedCoachMapper.getCoachByCoach(coachId);
        return coach;
    }

    @Override
    public List<Map<String, Object>> getTimeByCoach(int coachId) {
        List<Map<String, Object>> time = yogaGymSignedCoachMapper.getTimeByCoach(coachId);
        return time;
    }

    @Override
    public List<Map<String, Object>> selectCoach(int yoga_gym_id) {
        List<Map<String, Object>> list = yogaGymSignedCoachMapper.selectCoach(yoga_gym_id);
        return list;
    }
}
